package com.qj.yantaishan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.common.utils.StringUtils;
import com.qj.yantaishan.domain.CouponToProduct;
import com.qj.yantaishan.service.ICouponToProductService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.qj.yantaishan.mapper.CouponMapper;
import com.qj.yantaishan.domain.Coupon;
import com.qj.yantaishan.service.ICouponService;
import com.qj.yantaishan.common.core.text.Convert;

/**
 * 优惠券Service业务层处理
 *
 * @author qihe
 * @date 2022-07-22
 */
@Service
@Primary
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private ICouponToProductService couponToProductService;

    /**
     * 查询优惠券
     *
     * @param id 优惠券主键
     * @return 优惠券
     */
    @Override
    public Coupon selectCouponById(Long id) {
        return couponMapper.selectCouponById(id);
    }

    /**
     * 查询优惠券列表
     *
     * @param coupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon) {
        return couponMapper.selectCouponList(coupon);
    }

    /**
     * 新增优惠券
     *
     * @param coupon 优惠券
     * @return 结果
     */
    @Override
    public int insertCoupon(Coupon coupon) {
        return couponMapper.insertCoupon(coupon);
    }

    /**
     * 修改优惠券
     *
     * @param coupon 优惠券
     * @return 结果
     */
    @Override
    public int updateCoupon(Coupon coupon) {
        return couponMapper.updateCoupon(coupon);
    }

    /**
     * 批量删除优惠券
     *
     * @param ids 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteCouponByIds(String ids) {
        return couponMapper.deleteCouponByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除优惠券信息
     *
     * @param id 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteCouponById(Long id) {
        return couponMapper.deleteCouponById(id);
    }

    @Override
    public Map<String, Object> getCouponList(Integer currentPage, Integer pageSize, Integer status) {
        Map<String, Object> result = new HashMap<>();
        int current = (currentPage - 1) * pageSize;
        List<Coupon> coupons = couponMapper.selectCouponPage(current, pageSize, status);
        int total = couponMapper.total(status);
        result.put("current", currentPage);
        result.put("total", total);
        result.put("size", pageSize);
        result.put("pages", ((total + pageSize) - 1) / pageSize);
        result.put("records", coupons);
        return result;
    }

    @Override
    public void addCoupon(Coupon coupon) {
        save(coupon);
        if (coupon.getIsAllProduct() == 1) {
            //指定商品,获取商品id
            if (StringUtils.isNotEmpty(coupon.getCouponToProductIds())) {
                String couponToProductIds = coupon.getCouponToProductIds();
                //切割
                String[] ids = couponToProductIds.split(",");
                if (ids != null && ids.length > 0) {
                    for (String id : ids) {
                        CouponToProduct couponToProduct = new CouponToProduct();
                        couponToProduct.setProductId(Long.getLong(id));
                        couponToProduct.setCouponId(coupon.getId());
                        couponToProductService.save(couponToProduct);
                    }
                }
            }
        }
    }

    @Override
    public Object getCouponById(Long couponId) {
        return couponMapper.selectCouponById(couponId);
//        return null;
    }
}
